package com.rrd.mex.dw.mr.babel.parser;

import com.rrd.mex.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.hfq.UploadBaseData;
import com.ucredit.babel.common.model.hfq.phonebook.UploadPhoneBookData;
import com.ucredit.babel.common.model.hfq.phonebook.UploadPhoneBookDetail;
import com.ucredit.babel.common.model.hfq.phonebook.UploadPhoneBookRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 通讯录
 *
 * @author guoqiang
 */

public class MexPhoneBookRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 通讯录
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UploadPhoneBookRecord obj = (UploadPhoneBookRecord) model;
        UploadPhoneBookData data = obj.getData() == null ? new UploadPhoneBookData() : obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        UploadBaseData baseData = data.getBaseData();
        sb.append(baseData.getHttpTimestamp() == 0 ? "" : DateUtil.getUTC6(baseData.getHttpTimestamp())).append(CRT001);
        sb.append(baseData.getUploadInfoOpportunity() == null ? "" : baseData.getUploadInfoOpportunity()).append(CRT001);;
        sb.append(baseData.getVersion() == null ? "" : baseData.getVersion()).append(CRT001);
        sb.append(baseData.getIsAuthorized() == null ? "" : baseData.getIsAuthorized()).append(CRT001);
        sb.append(baseData.getOs() == null ? "" : baseData.getOs()).append(CRT001);

        List<UploadPhoneBookDetail> appDetails = data.getPhoneBookList();
        setFileNum(20);
        StringBuilder TxSb = new StringBuilder(500);
        for (UploadPhoneBookDetail app : appDetails) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(app.getName() == null ? "" : app.getName()).append(CRT001);
            TxSb.append(app.getMobile() == null ? "" : app.getMobile()).append(CRT001);
            TxSb.append(app.getCompany() == null ? "" : app.getCompany()).append(CRT001);
            TxSb.append(app.getPosition() == null ? "" : app.getPosition()).append(CRT001);
            TxSb.append(app.getEmail() == null ? "" : app.getEmail()).append(CRT001);
            TxSb.append(app.getPhoneSystem() == null ? "" : app.getPhoneSystem()).append(CRT001);
            TxSb.append(app.getPhoneType() == null ? "" : app.getPhoneType()).append(CRT001);
            TxSb.append(app.getCallCount() == null ? "" : app.getCallCount()).append(CRT001);
            TxSb.append(app.getCalledTime() == null ? "" : app.getCalledTime()).append(CRT001);
            TxSb.append(app.getCreateTime() == null ? "" : app.getCreateTime()).append(CRT001);
            TxSb.append(app.getModifyTime() == null ? "" : app.getModifyTime()).append(CRT001);
            TxSb.append(app.getRemark() == null ? "" : app.getRemark()).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (appDetails.size() == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_mex_phone_book_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.HFQ_UPLOAD_PHONE_BOOK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "通讯录";
    }

    @Override
    public BaseModel getModel() {
        return new UploadPhoneBookRecord();
    }
}
